package cn.yiyuan.car.controller;

import cn.yiyuan.base.until.AjaxResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @user m
 * @project Car_mall_system
 * @date 2024-01-09
 * @package cn.yiyuan.car.controller
 */
@RestController

public class TiangouController {
    @GetMapping("/tiangou")
    public AjaxResult tiangou() {
        String apiUrl = "https://v.api.aa1.cn/api/api-wenan-anwei/index.php";
        String type = "json"; // 替换为实际的参数值

        try {
            // 构建请求URL
//            String requestUrl = apiUrl ;
            String requestUrl = apiUrl + "?type=" + type;

            // 创建 URL 对象
            URL url = new URL(requestUrl);

            // 创建 HttpURLConnection 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // 获取响应代码
            int responseCode = connection.getResponseCode();

            // 如果响应代码是 200 表示请求成功
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 读取响应数据
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }

                // 关闭资源
                reader.close();

                // 解析 JSON 响应
//                parseJsonResponse(response.toString());
                return AjaxResult.me().setData(response.toString());

            } else {
                System.out.println("HTTP request failed with response code: " + responseCode);
            }

            // 关闭连接
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me();
    }
}

